//
//  MainViewController.m
//  Item A House
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "MainJumpViewController.h"
#import "HeadView.h"
#import "ServiceTableViewCell.h"
#import "RankModel.h"
#import "RankTableViewCell.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "FangDaiViewController.h"
#import "NewTwoViewController.h"
#import "OldViewController.h"
#import "RentalViewController.h"
#import "DWViewController.h"
#import "Singletion.h"

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

@implementation MainViewController
- (void)dealloc
{
    [_array release];
    [_table release];
    [_loupanid release];
    [super dealloc];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        
    }
    return self;
}
- (void)createNet
{
    Singletion *city = [Singletion shareManager];
    NSDictionary *dic = @{@"city_id":city.cityid};
    [SANetWorkingTask request:@"http://api.anjuke.com/xinfang/m/iphone/1.3/loupan/cityLoupanTop/" parmater:dic block:^(NSDictionary *result) {
        NSMutableDictionary *myDic = [result objectForKey:@"result"];
        NSMutableArray *myArr = [myDic objectForKey:@"rows"];
        for (NSDictionary *my in myArr) {
            RankModel *model = [[RankModel alloc] init];
            [model setValuesForKeysWithDictionary:my];
            [self.array addObject:model];
            [model release];
        }
        [self.table reloadData];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.translucent = NO;
    
    [self createNet];
    [self createTable];
    
    HeadView *head = [[HeadView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 0.5 + (5 + [UIScreen mainScreen].bounds.size.width / 4 - 35) + 20 + 12)];
    self.table.tableHeaderView = head;
    
    UIImageView *aImage = (UIImageView *)[head viewWithTag:1];
    UITapGestureRecognizer *recogn1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(newAction:)];
    [aImage addGestureRecognizer:recogn1];
    [recogn1 release];
    
    UIImageView *bImage = (UIImageView *)[head viewWithTag:2];
    UITapGestureRecognizer *recogn2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oldAction:)];
    [bImage addGestureRecognizer:recogn2];
    [recogn2 release];
    
    UIImageView *cImage = (UIImageView *)[head viewWithTag:3];
    UITapGestureRecognizer *recogn3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rentalAction:)];
    [cImage addGestureRecognizer:recogn3];
    [recogn3 release];
    
    UIImageView *dImage = (UIImageView *)[head viewWithTag:4];
    UITapGestureRecognizer *recogn4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calculatorAction:)];
    [dImage addGestureRecognizer:recogn4];
    [recogn4 release];
    [head release];
    
    
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    //申请释放池
    [self.table registerClass:[ServiceTableViewCell class] forCellReuseIdentifier:@"serviceCell"];
    [self.table registerClass:[RankTableViewCell class] forCellReuseIdentifier:@"rank"];
    [self.view addSubview:self.table];
    [self.table release];
    

//    UIImage *iageA = [[UIImage imageNamed:@"00.jpg"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    
//    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithImage:iageA  style:UIBarButtonItemStylePlain target:self action:@selector(barItemAction:)];
//    self.navigationItem.leftBarButtonItem = barItem;

}

#pragma mark -
#pragma mark dataSource
//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        return [self.array count];
    }
    return 0;
    
}
//tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ServiceTableViewCell *service = [tableView dequeueReusableCellWithIdentifier:@"serviceCell"];
        return service;
    }
    if (indexPath.section == 1) {
        RankTableViewCell *rank = [tableView dequeueReusableCellWithIdentifier:@"rank"];
        RankModel *model = [self.array objectAtIndex:indexPath.row];
        [rank.image sd_setImageWithURL:[NSURL URLWithString:model.default_image] placeholderImage:[UIImage imageNamed:@"10.jpg"]];;
        rank.loupan.text = model.loupan_name;
        rank.region.text = [NSString stringWithFormat:@"%@  %@", model.region_title, model.sub_region_title];
        rank.tags.text = model.tags;
        rank.price.text = [NSString stringWithFormat:@"%ld元/平", (long)model.price];
        return rank;
    }
    return nil;
    
    
    
}
//section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//题目
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        return @"新房优惠";
    }
    if (section == 1) {
        return @"人气楼盘排行榜";
    }
    return nil;
   
}
//跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否呼叫400-803-5055" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫", nil];
        [alert show];
        
//        [alert release];
    }
    
    if (indexPath.section == 1) {
        MainJumpViewController *jump = [[MainJumpViewController alloc] init];
        RankModel *model = [self.array objectAtIndex:indexPath.row];
        jump.loupan_id = model.loupan_id;
        jump.loupan_name = model.loupan_name;
        jump.rankModel = [self.array objectAtIndex:indexPath.row];
        jump.hidesBottomBarWhenPushed = YES;
        CATransition *trans = [CATransition animation];
        [self.navigationController pushViewController:jump animated:NO];
        trans.type = @"rippleEffect";
        trans.duration = 0.8f;
        [self.navigationController.view.layer addAnimation:trans forKey:nil];
        
        [jump release];
    }
}
- (NSString *)cancelBtnTitle
{
    return @"取消";
}

- (NSString *)otherBtnTitle
{
    return @"呼叫";
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([btnTitle isEqualToString:[self cancelBtnTitle]])
    {
       NSLog(@"点击取消");
    }
    else if ([btnTitle isEqualToString:[self otherBtnTitle]] )
    {
       NSLog(@"点击呼叫");
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://4008035055"]];
    }
}
#pragma mark -
#pragma mark delegate
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }
    if (indexPath.section == 1) {
        return 100;
    }
    return 150;
}

#pragma mark -
#pragma mark 跳转
- (void)newAction:(UITapGestureRecognizer *)tap
{
    NewTwoViewController *new = [[NewTwoViewController alloc] init];
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:new animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    [new release];
}
- (void)oldAction:(UITapGestureRecognizer *)tap
{
    OldViewController *old = [[OldViewController alloc] init];
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:old animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    old.title = @"二手房";
    [old release];
}

- (void)rentalAction:(UITapGestureRecognizer *)tap
{
    RentalViewController *rental = [[RentalViewController alloc] init];
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:rental animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    rental.title = @"租房";
    [rental release];
}

- (void)calculatorAction:(UITapGestureRecognizer *)tap
{
    FangDaiViewController *cal = [[FangDaiViewController alloc] init];
    cal.hidesBottomBarWhenPushed = YES;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:cal animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    [cal release];
}

@end
